let app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        images: [],
        plan_title: '请选择年份',
        plan_investment: '',
        plan_content: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({ projectId: options.id });
    },

    /** 计划投资金额 */
    inputTitle(e){
        this.setData({ plan_investment: e.detail.value });
    },

    /**  年度*/
    changeDate(e){
        this.setData({ plan_title: e.detail.value });
    },

    /** 输入内容 */
    inputArea(e){
        this.setData({ plan_content: e.detail.value });
    },

    onChoosePic(){
        let that = this;
        wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
            // tempFilePath可以作为img标签的src属性显示图片
            let tempFilePaths = res.tempFilePaths;
            wx.uploadFile({
            url: `${app.globalData.rootUrl}/api/upload/upload`,
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {'appid': app.appid},
            success: res => {
                let response = JSON.parse(res.data);
                if(res.statusCode == 200){
                if(response.code == 0){
                    let images = that.data.images;
                    images.push(response.thumb);
                    that.setData({ images });
                }else{
                    app.showFailToast(response.msg);
                }
                }else app.showFailToast('网络错误，请稍后再试');
            }
            });
        }
        });
    },

    /** 取消 */
    onCancel(){
        wx.navigateBack();
    },

    onSumit(){
        if(this.data.plan_title == '请选择年份'){
            app.showFailToast('请选择年份');
            return false;
        }
        if(this.data.plan_investment == ''){
            app.showFailToast('请填写计划投资金额');
            return false;
        }
        if(this.data.plan_content == ''){
            app.showFailToast('请填写内容');
            return false;
        }
        let params = {
            pro_id: this.data.projectId,
            plan_title: this.data.plan_title,
            plan_investment: this.data.plan_investment,
            plan_content: this.data.plan_content,
        };

        // console.log(params)
        app.request('post', '/api/Project/addplan', params).then(res => {
            app.showSuccessToast('提交成功');
            setTimeout(() => {
                wx.navigateBack();
            }, 1500);
        }).catch(err => console.log(err));
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})